package com.nervenets.template.controller.common.params;

import com.nervenets.general.jwt.util.JwtUtils;
import com.nervenets.general.model.SecurityUser;
import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.template.hibernate.domain.common.Address;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Setter
@Getter
@SuperBuilder
@AllArgsConstructor
public class AddressPagingParams extends EntityPagingParams<Address> {

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<Address> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        SecurityUser user = JwtUtils.getUser();
        addEqualsPredicate(root.get("userId"), user.getId(), true);
    }
}
